import os
import cgi
from google.appengine.api import mail
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson
from time import sleep

class Registros(db.Model):
	Nombre = db.StringProperty(multiline=False)
	APaterno = db.StringProperty(multiline=True)
	AMaterno = db.StringProperty(multiline=False)
	Mail = db.StringProperty(multiline=False)
	FecNacimiento = db.StringProperty(multiline=False)
	GradoMax = db.StringProperty(multiline=False)
	date = db.DateTimeProperty(auto_now_add=True)
	Evento=db.StringProperty(multiline=False)
	

class Register(webapp.RequestHandler):
	def post(self):							
		reg = Registros()
		reg.Nombre = self.request.get('Nombre')
		reg.Mail = self.request.get('Mail')
		reg.APaterno = self.request.get('APaterno')
		reg.AMaterno = self.request.get('AMaterno')
		reg.FecNacimiento = self.request.get('FecNacimiento')
		reg.GradoMax = self.request.get('GradoMax')
		reg.Evento = self.request.get('Evento')
		
		self.response.headers['Content-Type'] = 'text/html'
		if not mail.is_email_valid(reg.Mail):
			self.response.out.write('invalid email address')
			self.response.out.write(simplejson.dumps(result))			
		else:
			reg.put()
			mail.send_mail(sender="darkvander@gmail.com",
				to="contacto@laguna-gtug.org",
				subject="Nuevo Registro a Evento de Laguna-GTUG",
				body="Se agrego contacto:"+reg.Nombre+" "+reg.APaterno+" - "+reg.Mail)
				
			mail.send_mail(sender="darkvander@gmail.com",
				to=reg.Mail,
				subject="Bienvenido al evento de Laguna GTUG",
				body=reg.Nombre+" gracias por registrarte a nuestro nuevo evento! \n Happy Coding! ")
									
			self.response.out.write("<font face=Verdana size=-1>Registered succesfully. Don't forget your laptop. Happy Coding</font>")		

class Lista(webapp.RequestHandler):
	def get(self):
		self.response.headers['Content-Type'] = 'text/html'
		regs = db.GqlQuery("SELECT * FROM Registros")
		self.response.out.write("<font face='Verdana' size='-1'><h1>Registrados al Evento</h1><br><table>")					
		for reg in regs:			
			self.response.out.write("<tr><td>")
			self.response.out.write(reg.Nombre)
			self.response.out.write(" ")
			self.response.out.write(reg.APaterno)
			self.response.out.write(" ")
			self.response.out.write(reg.AMaterno)
			self.response.out.write("</td><td>")
			self.response.out.write(reg.Mail)
			self.response.out.write("</td><td>")
			self.response.out.write(reg.FecNacimiento)
			self.response.out.write("</td><td>")
			self.response.out.write(reg.GradoMax)
			self.response.out.write("</td><td>")
			self.response.out.write(reg.Evento)
			self.response.out.write("</td></tr>")
		self.response.out.write("</table></font>")	
			
	
class Borra(webapp.RequestHandler):
	def get(self):
		self.response.headers['Content-Type'] = 'text/html' 
		regs = db.GqlQuery("SELECT * "
                            "FROM Registros WHERE APaterno='Urby'")
		for reg in regs:
			reg.delete()
				
				
class Invita(webapp.RequestHandler):
	def get(self):
		self.response.headers['Content-Type'] = 'text/html' 
		regs = db.GqlQuery("SELECT * "
                            "FROM Registros")
		enviados = 0
		for reg in regs:			
			self.response.out.write("Correo enviado a "+reg.Mail+"<br>")	
			enviados += 1
			if enviados == 32:
				enviados = 0
				self.response.out.write("Enviados=32<br>Esperando 1 minuto <br>")	
				time.sleep(120)

				
class MainPage(webapp.RequestHandler):
  def get(self):
		self.response.headers['Content-Type'] = 'text/html'
		path = os.path.join(os.path.dirname(__file__), 'registro.html')
		f = open(path,"r")
		self.response.out.write(f.read())			
		
application = webapp.WSGIApplication(
                                     [('/', MainPage),
									  ('/Register', Register),
									  ('/List', Lista),
									  ('/Borra', Borra),
									  ('/Invite', Invita)
									 ],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
